package com.hlx.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlx.entity.admin.Price;
import com.hlx.entity.admin.Station;
import com.hlx.entity.admin.TrainNumber;
import com.hlx.entity.admin.TrainNumberSeat;
import com.hlx.entity.user.TrainNumberStation;
import com.hlx.service.admin.StationService;
import com.hlx.service.admin.TrainNumberSeatService;
import com.hlx.service.admin.TrainNumberService;
import com.hlx.service.admin.TrainService;
import com.hlx.service.user.OrderFormService;
import com.hlx.service.user.TicketService;
import com.hlx.service.user.TrainNumberStationService;
import com.hlx.utils.Result;
import com.hlx.vo.TrainNumberVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/trainNumber")
public class TrainNumberController {
    @Autowired
    private TrainNumberService trainNumberService;
    @Autowired
    private StationService stationService;
    @Autowired
    private TrainService trainService;
    @Autowired
    private TrainNumberStationService trainNumberStationService;
    @Autowired
    private TrainNumberSeatService trainNumberSeatService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private OrderFormService orderFormService;

    @GetMapping("/getAllTrainNumberByPage/{pageSize}/{currentPage}")
    public Result<Page> getAllTrainNumberByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage){
        //        分页构造
        Page page = new Page(currentPage,pageSize);
        return Result.success(trainNumberService.page(page));
    }

    @GetMapping("/getTrainNumberByPage/{pageSize}/{currentPage}/{search}")
    public Result<Page> getTrainNumberByPage(@PathVariable Integer pageSize, @PathVariable Integer currentPage,@PathVariable String search){
        //        分页构造
        LambdaQueryWrapper<TrainNumber> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(TrainNumber::getTrainNumberName,search);
        Page page = new Page(currentPage,pageSize);
        return Result.success(trainNumberService.page(page,queryWrapper));
    }

    @PostMapping("/addTrainNumber")
    @Transactional
//    事务管理
    public Result<String> addTrainNumber(@RequestBody TrainNumberVo trainNumberVo){
        log.info("{}",trainNumberVo);
//      组装trainNumber,前端往后传递的数据缺少end_time,end_date,组装完成后直接插入
        List<TrainNumberStation> trainNumberStations = trainNumberVo.getStations();
        TrainNumber trainNumber = new TrainNumber();
        trainNumber.setTrainNumber(trainNumberVo);
        trainNumber.setStationStart(stationService.getById(trainNumber.getStationStart()).getStationName());
        trainNumber.setStationEnd(stationService.getById(trainNumber.getStationEnd()).getStationName());
        trainNumber.setStartDate(trainNumber.getStartDate().plusDays(1));
        TrainNumber trainNumberFin = trainNumberService.addTrainNumber(
                trainNumber,
                stationService.getDistance(trainNumber.getStationStart(), trainNumber.getStationEnd()),
                trainService.getTrainVelocity(trainNumber.getTrainType()),
                trainNumberStationService.getStopTime(trainNumberStations));
//      组装trainNumberStation 前端只往后传了车站id与stopTime
        List<String> stationNames = trainNumberStations.stream().map(item->item.getStationName()).collect(Collectors.toList());
        List<Integer> stopTimes = trainNumberStations.stream().map(item->item.getStopTime()).collect(Collectors.toList());
        stationNames.add(0,trainNumberFin.getStationStart());
        stationNames.add(stationNames.size(),trainNumberFin.getStationEnd());
        List<Station> stations = stationService.getAllStationByName(stationNames);
        System.out.println(stationNames);
        trainNumberStationService.addTrainNumberStations(stations,trainNumberFin,trainService.getTrainVelocity(trainNumber.getTrainType()),stopTimes);
//      插入对应的座位数
        trainNumberSeatService.addTrainNumberSeat(trainNumberFin,trainService.getTrainByTrainName(trainNumber.getTrainType()));
        return Result.success("添加成功");
    }

    @DeleteMapping("/deleteTrainNumber/{id}/{trainNumberName}")
    @Transactional
    public Result<String> deleteTrainNumber(@PathVariable Integer id,@PathVariable String trainNumberName) {
        orderFormService.deleteOrderByTicketIds(ticketService.getAllTicketIdByTrainNumberId(id));
        ticketService.deleteTicketByTrainId(id);
        trainNumberSeatService.deleteTrainNumberByTrainNumberId(id);
        trainNumberStationService.deleteTrainNumberStations(trainNumberName);
        trainNumberService.removeById(id);
        return Result.success("删除成功");
    }

}
